home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 1 of 3 / CHAPTE13 / SETDIBTD.C < prev    next >
C/C++ Source or Header  |  1996-01-31  |  9KB  |  269 lines

  1.  
  2. #include <windows.h>  
  3. #include "SetDIBTD.h" 
  4.  
  5.  
  6. #if defined (WIN32)
  7.     #define IS_WIN32 TRUE
  8. #else
  9.     #define IS_WIN32 FALSE
  10. #endif
  11.  
  12. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  13. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  14. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  15.  
  16. HINSTANCE hInst;   // current instance
  17.  
  18. LPCTSTR lpszAppName  = "MyApp";
  19. LPCTSTR lpszTitle    = "My Application"; 
  20.  
  21. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  22.  
  23. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  24.                       LPTSTR lpCmdLine, int nCmdShow)
  25. {
  26.    MSG      msg;
  27.    HWND     hWnd; 
  28.    WNDCLASS wc;
  29.  
  30.    // Register the main application window class.
  31.    //............................................
  32.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  33.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  34.    wc.cbClsExtra    = 0;                      
  35.    wc.cbWndExtra    = 0;                      
  36.    wc.hInstance     = hInstance;              
  37.    wc.hIcon         = LoadIcon( hInstance, lpszAppName ); 
  38.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  39.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  40.    wc.lpszMenuName  = lpszAppName;              
  41.    wc.lpszClassName = lpszAppName;              
  42.  
  43.    if ( IS_WIN95 )
  44.    {
  45.       if ( !RegisterWin95( &wc ) )
  46.          return( FALSE );
  47.    }
  48.    else if ( !RegisterClass( &wc ) )
  49.       return( FALSE );
  50.  
  51.    hInst = hInstance; 
  52.  
  53.    // Create the main application window.
  54.    //....................................
  55.    hWnd = CreateWindow( lpszAppName, 
  56.                         lpszTitle,    
  57.                         WS_OVERLAPPEDWINDOW, 
  58.                         CW_USEDEFAULT, 0, 
  59.                         CW_USEDEFAULT, 0,  
  60.                         NULL,              
  61.                         NULL,              
  62.                         hInstance,         
  63.                         NULL               
  64.                       );
  65.  
  66.    if ( !hWnd ) 
  67.       return( FALSE );
  68.  
  69.    ShowWindow( hWnd, nCmdShow ); 
  70.    UpdateWindow( hWnd );         
  71.  
  72.    while( GetMessage( &msg, NULL, 0, 0) )   
  73.    {
  74.       TranslateMessage( &msg ); 
  75.       DispatchMessage( &msg );  
  76.    }
  77.  
  78.    return( msg.wParam ); 
  79. }
  80.  
  81.  
  82. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  83. {
  84.    WNDCLASSEX wcex;
  85.  
  86.    wcex.style         = lpwc->style;
  87.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  88.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  89.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  90.    wcex.hInstance     = lpwc->hInstance;
  91.    wcex.hIcon         = lpwc->hIcon;
  92.    wcex.hCursor       = lpwc->hCursor;
  93.    wcex.hbrBackground = lpwc->hbrBackground;
  94.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  95.    wcex.lpszClassName = lpwc->lpszClassName;
  96.  
  97.    // Added elements for Windows 95.
  98.    //...............................
  99.    wcex.cbSize = sizeof(WNDCLASSEX);
  100.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  101.                             IMAGE_ICON, 16, 16,
  102.                             LR_DEFAULTCOLOR );
  103.             
  104.    return RegisterClassEx( &wcex );
  105. }
  106.  
  107. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  108. {
  109. static HPALETTE hPalette;
  110. static int      nColorData;
  111.  
  112.    switch( uMsg )
  113.    {
  114.       case WM_CREATE  :
  115.               {
  116.                  HANDLE       hRes, hPal;
  117.                  LPBITMAPINFO lpBi;
  118.                  LPLOGPALETTE lpPal;
  119.                  int          i;
  120.  
  121.                  // Load the bitmap.
  122.                  //.................
  123.                  hRes = LoadResource( hInst, 
  124.                             FindResource( hInst, "testdib", RT_BITMAP ) );
  125.  
  126.                  lpBi = (LPBITMAPINFO)LockResource( hRes );
  127.  
  128.                  // Find out how many colors we need.
  129.                  //..................................
  130.                  if ( lpBi->bmiHeader.biClrUsed != 0 )
  131.                     nColorData = lpBi->bmiHeader.biClrUsed;
  132.                  else
  133.                     switch( lpBi->bmiHeader.biBitCount )
  134.                     {
  135.                        case 1  : nColorData = 2;   break; // Monochrome
  136.                        case 4  : nColorData = 16;  break; // VGA
  137.                        case 8  : nColorData = 256; break; // SVGA
  138.                        case 24 : nColorData = 0;   break; // True Color
  139.                     }
  140.  
  141.                  // Allocate memory for color palette.
  142.                  //...................................
  143.                  hPal = GlobalAlloc( GHND, sizeof(LOGPALETTE)+
  144.                                         (nColorData * sizeof(PALETTEENTRY)) );
  145.                  lpPal = (LPLOGPALETTE)GlobalLock( hPal );
  146.  
  147.                  lpPal->palVersion    = 0x300;      
  148.                  lpPal->palNumEntries = nColorData;
  149.  
  150.                  // Load each color into the palette.
  151.                  //..................................
  152.                  for ( i = 0; i < nColorData; i++ )
  153.                  {
  154.                     lpPal->palPalEntry[i].peRed   = lpBi->bmiColors[i].rgbRed;
  155.                     lpPal->palPalEntry[i].peGreen = 
  156.                                                   lpBi->bmiColors[i].rgbGreen;
  157.                     lpPal->palPalEntry[i].peBlue  = 
  158.                                                   lpBi->bmiColors[i].rgbBlue;
  159.                  }
  160.  
  161.                  // Create the Palette.
  162.                  //....................
  163.                  hPalette = CreatePalette( lpPal );
  164.  
  165.                  GlobalUnlock( hPal );
  166.                  GlobalFree( hPal );
  167.               }
  168.               break;
  169.  
  170.       case WM_PAINT :
  171.               {
  172.                  PAINTSTRUCT  ps;
  173.                  LPBITMAPINFO lpBi;
  174.                  HANDLE       hRes;
  175.                  LPTSTR       lpBits;
  176.  
  177.                  BeginPaint( hWnd, &ps );
  178.  
  179.                  // Select palette and realize it.
  180.                  //...............................
  181.                  SelectPalette( ps.hdc, hPalette, FALSE );
  182.                  RealizePalette( ps.hdc );
  183.  
  184.                  // Load the bitmap.
  185.                  //.................
  186.                  hRes = LoadResource( hInst, 
  187.                             FindResource( hInst, "testdib", RT_BITMAP ) );
  188.  
  189.                  lpBi = (LPBITMAPINFO)LockResource( hRes );
  190.  
  191.                  // Locate the bitmap data.
  192.                  //........................
  193.                  lpBits =  (LPSTR) lpBi;
  194.                  lpBits +=  lpBi->bmiHeader.biSize + 
  195.                             ( nColorData*sizeof(RGBQUAD) );
  196.  
  197.                  // Paint the bitmap normal size.
  198.                  //..............................
  199.                  SetDIBitsToDevice( ps.hdc, 10, 10, 
  200.                                             lpBi->bmiHeader.biWidth,
  201.                                             lpBi->bmiHeader.biHeight,
  202.                                             0, 0,
  203.                                             0, lpBi->bmiHeader.biHeight,
  204.                                             lpBits, lpBi, DIB_RGB_COLORS );
  205.  
  206.                  // Paint the bitmap 200% size.
  207.                  //............................
  208.                  StretchDIBits( ps.hdc, 40+lpBi->bmiHeader.biWidth, 10,
  209.                                         lpBi->bmiHeader.biWidth*2,
  210.                                         lpBi->bmiHeader.biHeight*2,
  211.                                         0, 0,
  212.                                         lpBi->bmiHeader.biWidth,
  213.                                         lpBi->bmiHeader.biHeight,
  214.                                         lpBits, lpBi, DIB_RGB_COLORS, 
  215.                                         SRCCOPY );
  216.  
  217.                  EndPaint( hWnd, &ps );
  218.               }
  219.               break;
  220.  
  221.       case WM_COMMAND :
  222.               switch( LOWORD( wParam ) )
  223.               {
  224.                  case IDM_ABOUT :
  225.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  226.                         break;
  227.  
  228.                  case IDM_EXIT :
  229.                         DestroyWindow( hWnd );
  230.                         break;
  231.               }
  232.               break;
  233.       
  234.       case WM_DESTROY :
  235.               DeleteObject( hPalette );
  236.               PostQuitMessage(0);
  237.               break;
  238.  
  239.       default :
  240.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  241.    }
  242.  
  243.    return( 0L );
  244. }
  245.  
  246.  
  247. LRESULT CALLBACK About( HWND hDlg,           
  248.                         UINT message,        
  249.                         WPARAM wParam,       
  250.                         LPARAM lParam)
  251. {
  252.    switch (message) 
  253.    {
  254.        case WM_INITDIALOG: 
  255.                return (TRUE);
  256.  
  257.        case WM_COMMAND:                              
  258.                if (   LOWORD(wParam) == IDOK         
  259.                    || LOWORD(wParam) == IDCANCEL)    
  260.                {
  261.                        EndDialog(hDlg, TRUE);        
  262.                        return (TRUE);
  263.                }
  264.                break;
  265.    }
  266.  
  267.    return (FALSE); 
  268. }
  269.